#!/usr/bin/python3
import os
from .Entity import Entity
from wrappers.OpenAPI_Wrapper import get_api_model_annotation, get_includes
from utils import My_Util2


class Entity_BO:
    """
    实体新增模型
    """

    def __init__(self, context, entity: Entity) -> None:
        self.context = context
        self.entity: Entity = entity
        self.prefix: str = self.entity.table_name.split("_")[0].lower()
        self.my_util2 = My_Util2.My_Util(self.context)

    def code_includes(self):
        package = self.entity.package
        entity_name = self.entity.name
        prefix = self.prefix
        prefix_first_upper = "{}{}".format(prefix[0].upper(), prefix[1:])
        suffix_name = entity_name[
            entity_name.find(prefix_first_upper) + len(prefix_first_upper) :
        ].lower()
        i18n = False
        currency = False
        decimal = False
        date = False
        for field in self.entity.fields:
            if field.ignore():
                continue
            if field.i18n:
                i18n = True
            if field.currency:
                currency = True
            if field.java_type == "BigDecimal":
                decimal = True
            if field.java_type == "Date":
                date = True

        includes = list()
        includes.append(f"""import {package}.internal.bo.{entity_name}BaseBO;""")
        if currency:
            includes.append(f"""import {package}.internal.currency.Currency;""")
        if i18n:
            includes.append(f"""import {package}.internal.i18n.NameInfo;""")
        includes += get_includes("model")
        includes.append(f"""import lombok.Data;""")
        includes.append(f"""import lombok.EqualsAndHashCode;""")
        includes.append(f"""import lombok.ToString;""")
        includes.append(f"""import lombok.experimental.Accessors;""")
        if decimal:
            includes.append("import java.math.BigDecimal;")
        if date:
            includes.append("import java.util.Date;")
        includes = self.my_util2.sort_import_order(includes)
        includes = "\r".join(includes)
        return includes

    def code_fields(self):
        field_code = list()
        for field in self.entity.fields:
            if field.ignore() or field.is_status():
                continue
            field_code.append(field.code())
        return field_code

    def save_file(self, dir):
        _target_dir = os.path.join(dir, self.prefix, "bo")
        if not os.path.exists(_target_dir):
            os.makedirs(_target_dir)
        with open(
            os.path.join(_target_dir, "{}BO.java".format(self.entity.name)),
            mode="w",
            encoding="utf-8",
        ) as f:
            f.write(self.code())

    def code(self):
        package = self.entity.package
        entity_name = self.entity.name
        comment = self.entity.comment
        author = self.entity.author
        prefix = self.prefix

        includes = self.code_includes()

        return f"""package {package}.{prefix}.bo;

{includes}

/**
 * <p>
 * {comment}业务对象
 * </p>
 *
 * @author {author}
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
@ToString(callSuper = true)
{get_api_model_annotation(f"""{entity_name}BO""", comment)}
public final class {entity_name}BO extends {entity_name}BaseBO {"{"}

{"}"}
"""
